﻿using UnityEngine;
using System.Collections;

public class ScreenControl : MonoBehaviour {
	[SerializeField]
	GameObject _characterModel = null;
	[SerializeField]
	Camera _Camera = null;
	[SerializeField]
	public float _speedMove=0.0f;
	[SerializeField]
	public float _speedRotation=0.0f;
	[SerializeField]
	public UIToolkit _TkMenu;
	[SerializeField]
	public Animation _animation;
    [SerializeField]
    public float _minAngle = 20.0f;
    [SerializeField]
    public float _maxAngle= 70.0f;
    [SerializeField]
    public float _camDistanceW = 28.0f;
    [SerializeField]
    public float _camDistanceH = 30.0f;

	private Transform _cachedTransform;
    private UIJoystick leftJoystick;
	private ScreenControlViewModel vmScreenControl;
	private bool _canRotate = false;
	private bool _canMove = false;
	private Vector2 DeadZone = new Vector2(0.99f,0.99f);
    
    private CharacterModel chModel;
	public bool CanRotate
	{
		get { return _canRotate; } 
		private set { _canRotate = value; } 
	}
	
	public bool CanMove
	{
		get { return _canMove;}	
		private set { _canMove = value; } 
	}

	void Start () {
        chModel = _characterModel.GetComponent<CharacterModel>();
        vmScreenControl = new ScreenControlViewModel(_characterModel, _Camera, chModel.cController,chModel.bxcCollider);
        vmScreenControl.SetCameraProperties(_minAngle,_maxAngle,_camDistanceW,_camDistanceH);
        vmScreenControl.SpeedMove = _speedMove;
        vmScreenControl.SpeedRotation = _speedRotation;
        vmScreenControl.StartCamera();
		CreateControls();
		ConfigureControls();
	}
	
	private void CreateControls()
	{
        Rect HitArea = new Rect(0, ScreenGeneral.pieceY * 0.6f, ScreenGeneral.pieceX / 2f, ScreenGeneral.pieceY*0.7f);
        //Rect HitArea = new Rect(ScreenGeneral.pieceX * 0.01f, ScreenGeneral.pieceY * 0.7f, ScreenGeneral.pieceX * 0.15f, ScreenGeneral.pieceY * 0.3f);
		//leftJoystick = UIJoystick.create(_TkMenu, "Joystick.png", new Rect(ScreenGeneral.pieceX*0.01f, ScreenGeneral.pieceY*0.7f, ScreenGeneral.pieceX*0.15f, ScreenGeneral.pieceY*0.3f ),64,-64);
        leftJoystick = UIJoystick.create(_TkMenu, "Knob.png", HitArea, ScreenGeneral.pieceX*0.1f,-HitArea.height*0.4f);
	}
	
	private void ConfigureControls()
	{
        leftJoystick.beginUpdates();
        leftJoystick.deadZone = DeadZone;
        leftJoystick.allowTouchBeganWhenMovedOver = true;
        leftJoystick.addBackgroundSprite("Joystick.png");
        leftJoystick.maxJoystickMovement = 30.0f;
        leftJoystick.endUpdates();
        
	}
		
	private bool start=false;
	
	void LateUpdate()
	{
		if(start){
			vmScreenControl.MoveObject(leftJoystick);
		}
	}
	
	void OnGUI () {	
		if(Input.GetMouseButtonDown(0)){
			start=true;
		}
		if(Input.GetMouseButtonUp(0))
		{
			start=false;
		}
		if(Input.touchCount > 0)
		{
			for(int i=0;i<Input.touchCount;i++){
				switch(Input.GetTouch(i).phase)
				{
					case TouchPhase.Began:	
						VerifyTouch();
					break;
					case TouchPhase.Moved:	
						if(CanMove)
						{
                            _animation.Play("Walk");
							vmScreenControl.MoveObject(leftJoystick);
						}
					break;
					case TouchPhase.Stationary:
						if(CanMove){
                            _animation.Play("Walk");
							vmScreenControl.MoveObject(leftJoystick);
						}
					break;
					case TouchPhase.Ended:	
						CanMove = false;
                        _animation.Stop("Walk");
                        _animation.Play("Idle");
					break;
				}
			}
		}
	}
	
	private void VerifyTouch()
	{
		if (leftJoystick.highlighted == true) 
				CanMove = true;
	}
}
